<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/28
 * Time: 22:37
 */

namespace app\index\controller;
use cart\Cart;
use think\Controller;

/**
 * Class Pog
 * @package app\index\controller
 * 商品详情控制器
 */
class Pog extends Controller
{
    //首页
    public function index(){
        $gid = input('param.gid');
        $conf = ['title'=>'阿里巴巴1688.com - 商品详情',
        'css'=>'pog','js'=>'pog'];
        $this->assign('conf',$conf);
        //获取商品详情
        $pogData = db('pog')
                ->alias('p')
                ->join('__GOODS__ g','p.gid = g.gid')
                ->where('p.gid',$gid)
                ->select();
        foreach ($pogData as $k => $v) {
            $pogData[$k]['small'] = explode(',', $v['small']);
        }
        foreach ($pogData as $k => $v) {
            $pogData[$k]['medium'] = explode(',', $v['medium']);
        }
        foreach ($pogData as $k => $v) {
            $pogData[$k]['big'] = explode(',', $v['big']);
        }

//
        //1通过商品id来获取对应的类型属性
        $specData = db('type-attribute')
            ->alias('ta')
            ->join('__GOODS__ g', 'ta.tid = g.tid')
            ->where('g.gid', $gid)
            ->where('class', 1)
            ->field('ta.taid,ta.taname')
            ->select();
        //2.通过taid获取对应商品的选中规格
        foreach ($specData as $k => $v) {
            $specData[$k]['select'] = db('goods-attribute')
                ->where('taid', $v['taid'])
                ->where('gid', $gid)
                ->field(['gaid,gtvalue'])
                ->select();
        }
        $this->assign('specData', $specData);

        //1通过商品id来获取对应的类型属性
        $attrData = db('type-attribute')
            ->alias('ta')
            ->join('__GOODS__ g', 'ta.tid = g.tid')
            ->where('g.gid', $gid)
            ->where('class', 2)
            ->field('ta.taid,ta.taname')
            ->select();
        //2.通过taid获取对应商品的选中规格
        foreach ($attrData as $k => $v) {
            $attrData[$k]['select'] = db('goods-attribute')
                ->where('taid', $v['taid'])
                ->where('gid', $gid)
                ->field(['gaid,gtvalue'])
                ->select();
        }
        $this->assign('attrData', $attrData);

        $this->assign('pogData',$pogData);
        //视图
        return view();
    }
    //添加购物车
    public function addCart(){
        $data = input('post.');
        //获取商品信息
        $goodsData = db('goods')->find($data['gid']);
        //加入购物车
        $data = [
            'id' 		=> $data['gid'], // 商品 ID
            'name'		=> $goodsData['gname'],// 商品名称
            'num' 		=> $data['number'], // 商品数量
            'price' 	=> $goodsData['shop_price'], // 商品价格
            'options'   => $data['spec'],// 其他参数如价格、颜色、可以为数组或字符串
            'pic'		=> $goodsData['pic']//商品logo
        ];
        (new Cart())->add($data); // 添加到购物车
        echo 1;
    }
}